187. 重复的DNA序列

187. 重复的DNA序列

Similar Question

leading to the advanced question

Solution Tips

出现不止一次的长度为 10 的子串

方案一: 滑动窗口 + 哈希

var findRepeatedDnaSequences = function(s) {
    // 滑动窗口, 哈希表? 因为是字符串, 所以可以
    const map = {};
    const res = [];
    let i = 0;
    let j = i + 10;

    while (i <= s.length - 10) {
        const sub = s.slice(i, j);
        if (map.hasOwnProperty(sub)) {
            map[sub] && res.push(sub);
            map[sub] = false;
        }
        else {
            map[sub] = true;
        }

        i++;
        j++;
    }

    return res;
};